﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using System.Configuration;
using System.Globalization;

namespace SW_SoftBully.Models.ModuloServicios.CuotaExtraordinaria
{
    public class CuotaExtraordinariaDAO
    {
         public string RegistrarCuotaExtraordinaria(CuotaExtraordinariaBean cuota) {
            try{
                String cadenaConfiguracion = ConfigurationManager.ConnectionStrings["SoftBully"].ConnectionString;

                SqlConnection sqlCon = new SqlConnection(cadenaConfiguracion);
                
                sqlCon.Open();
                int estadonum=0;
                //string XD2 = cuota.FechaI.ToShortDateString();
                //System.Diagnostics.Debug.WriteLine("--------------------------------------> llego" + xD + " " + XD2);
                
                string commandString = "INSERT INTO Cuota_Extraordinaria (Monto,Motivo,fecha_inicio,fecha_fin,Estado,id_Edificio) " +
                    "VALUES (" + cuota.MontoTotal + ",'" + cuota.TextAreaCuotaExtraordinaria + "'," + "convert(datetime,'" + cuota.FechaI + "',103),convert(datetime,'"+cuota.FechaF +  "',103)," + cuota.Estado + ", " + "2" + ")";
                System.Diagnostics.Debug.WriteLine("--------------------------------------> llego" + commandString);
                SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
                sqlCmd.ExecuteNonQuery();

                sqlCon.Close();

                cuota.conexion = "Bien";
                
            }

            catch{
                cuota.conexion = "Conexion Fallida";
            }

            return cuota.conexion;
        }

         public List<CuotaExtraordinariaBean> ListarCuotas(double montoTotal, string motivo, string fechaIni, string fechaFin, 
            int id_Edificio, int estado)
        {
            try
            {
                List<CuotaExtraordinariaBean> listaCuotas = new List<CuotaExtraordinariaBean>();

                String cadenaConfiguracion = ConfigurationManager.ConnectionStrings["SoftBully"].ConnectionString;

                SqlConnection sqlCon = new SqlConnection(cadenaConfiguracion);

                sqlCon.Open();

                string commandString = "SELECT * FROM Cuota_Extraordinaria where";

                SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);

                SqlDataReader dataReader = sqlCmd.ExecuteReader();

                while (dataReader.Read())
                {
                    CuotaExtraordinariaBean cuota = new CuotaExtraordinariaBean();
                    cuota.MontoTotal = Double.Parse((string)dataReader["MontoTotal"]);
                    cuota.FechaI= (string)dataReader["Fecha Inicial"];
                    cuota.FechaF = (string)dataReader["Fecha Final"];
                    cuota.id_Edificio= Int32.Parse((string)dataReader["id_Edificio"]);
                    cuota.Estado= Int32.Parse((string)dataReader["estado"]);
                    cuota.TextAreaCuotaExtraordinaria= (string)dataReader["TextAreaCuotaExtraordinaria"];
                    listaCuotas.Add(cuota);
                }

                dataReader.Close();
                sqlCon.Close();
                return listaCuotas;
            }
            catch {
                return null;
            }
        }
    }
}

